package cup.com.icss.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.dao.DormitoryMapper;
import cup.com.icss.model.Dormitory;
import cup.com.icss.model.PeopleRecord;
import cup.com.icss.model.User;
import cup.com.icss.utils.LogUtil;
import cup.com.icss.utils.StringUtil;

@Controller
public class dormitoryController {
	@Resource
	private DormitoryMapper dormitoryMapper;
	
	@RequestMapping("/dormitoryInitialize")
	@ResponseBody
	public ModelAndView dormitoryInitialize(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("storiedBuilding",user.getStoriedBuilding());
			view.setViewName("cup/jsp/dormitory/dormitory");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	@RequestMapping(value="/dormitoryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> dormitoryList(HttpServletRequest request, Integer page, Integer rows, Dormitory record){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			page=page==null?1:page;
			rows=rows==null?10:rows;
			int pageNum = page;
			int pageSize = rows;
			int num = dormitoryMapper.dormitoryListSize(record).size();
			int pageSizez;
			if(((pageSize*pageNum)-num)<0){
				pageSizez = pageSize;
			}else{
				pageSizez = num-(10*(pageNum-1));
			}
			List<Dormitory> somethingLost = dormitoryMapper.dormitoryList(pageNum*pageSize-pageSize,pageSizez,record);
			map.put("total", num);
			map.put("rows", somethingLost);
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/dormitoryAdd",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView dormitoryAdd(HttpServletRequest request,Dormitory record){
		ModelAndView view = new ModelAndView();
		try {
			view.setViewName("cup/jsp/dormitory/dormitoryAdd");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/dormitoryAddData",method=RequestMethod.POST)
	@ResponseBody 
	public Map<String, String> dormitoryAddData(HttpServletRequest request,Dormitory record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			record.setStoriedBuilding(user.getStoriedBuilding());
			record.setId(StringUtil.getUUID());
			int inster = dormitoryMapper.insertSelective(record);
			if(inster>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/dormitoryEdit",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView dormitoryEdit(HttpServletRequest request,String id){
		ModelAndView view = new ModelAndView();
		try {
			Dormitory dormitory = dormitoryMapper.selectByPrimaryKey(id);
			view.addObject("dormitory",dormitory);
			view.setViewName("cup/jsp/dormitory/dormitoryEdit");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/dormitoryEditData",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> dormitoryEditData(HttpServletRequest request,Dormitory record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int updata = dormitoryMapper.updateByPrimaryKeySelective(record);
			if(updata>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/dormitoryDel",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> dormitoryDel(HttpServletRequest request,String id){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int del = dormitoryMapper.deleteByPrimaryKey(id);
			if(del>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
}
